<template>
  <div>
    <van-cell-group>
      <van-field type="text" v-model="leave.name" label="请假人" disabled input-align="right"/>
      <van-field type="text" v-model="leave.course" label="请假课程" disabled input-align="right"/>
      <van-field type="text" v-model="leave.time" label="请假时间" disabled input-align="right"/>
    </van-cell-group>
    <div class="item sae-textarea">
      <van-cell-group>
        <van-field type="textarea" rows="5" autosize v-model="form.reason" label="请假原因"/>
      </van-cell-group>
    </div>
    <div class="btns">
      <van-button type="info" hairline plain @click="$router.go(-1)">返回</van-button>
      <van-button type="info" @click="toSubmit">提交</van-button>
    </div>
    <van-popup v-model="showDate" position="bottom">
      <van-datetime-picker
        type="date"
        @cancel="dateCancel"
        @confirm="dateConfirm"
        :min-date="minDate"/>
    </van-popup>
  </div>
</template>

<script>
import {birthdayHandle} from '@/utils'
import api from '@/api'
export default {
  name: 'leave',
  data () {
    return {
      leave: {
        name: '',
        course: '',
        time: '',
        des: ''
      },
      minDate: new Date(),
      showDate: false,
      form: {
        reason: ''
      }
    }
  },
  methods: {
    dateCancel () {
      this.showDate = false
    },
    dateConfirm (value) {
      this.leave.time = birthdayHandle(value)
      this.showDate = false
    },
    toSubmit () {
      console.log(this.form)
      api.studentLeave({
        params: this.form,
        callback: res => {
          if (res.data.error_code === 0) {
            this.$toast({
              message: '请假成功，等待教师审核',
              duration: 1000,
              forbidClick: false,
              onClose: () => {
                this.$router.go(-1)
              }
            })
          }
        }
      })
    }
  },
  created () {
    this.leave.name = JSON.parse(sessionStorage.getItem('user')).name
    this.leave.time = birthdayHandle(new Date(Number(this.$route.query.time) * 1000))
    this.leave.course = this.$route.query.name
    this.form.sktime = this.$route.query.time
    this.form.student_id = JSON.parse(sessionStorage.getItem('user')).id
    this.form.profile_id = this.$route.query.id
  }
}
</script>

<style scoped>
  .item{
    border-top: 10px solid #F2F2F2;
  }
  .btn{
    margin-top: 30px;
    display: flex;
    justify-content: space-around;
  }
</style>
